<?php
/**
 * 用户绑定角色
 * User: Administrator
 * Date: 2018\9\10 0010
 * Time: 16:26
 */

namespace app\Lib\Model\User;


use app\Lib\Exception\BlException;
use app\Lib\Exception\SysException;
use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\User\UserBindingRoleHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\RoleUser;

class UserBindingRole implements UserBindingRoleHandle
{   /** @var UserHandle $userObj */
    private $userObj;
    /** @var UserHandle $opUserObj */
    private $opUserObj;
    /** @var RoleHandle $roleObj */
    private $roleObj;
    private $roleBindingUserDb;

    /**
     * 初始化
     * UserBindingRoleBaseHandle constructor.
     * @param UserHandle $userObj
     * @throws SysException
     */
    public function __construct(UserHandle $userObj)
    {
        if(!($userObj instanceof  UserHandle)){
            throw new SysException('实例化用户类失败');
        }
        $this->userObj = $userObj;
    }

    /**
     * 设置操作用户类
     * @param UserHandle $opUserObj
     * @return $this
     */
    public function setOpUserObj(UserHandle $opUserObj)
    {
        $this->opUserObj = $opUserObj;
        return $this;
    }

    /**
     * 设置要绑定的角色类
     * @param RoleHandle $roleObj
     * @return $this
     */
    public function setRoleObj(RoleHandle $roleObj)
    {
        $this->roleObj = $roleObj;
        return $this;
    }

    /**
     * 执行
     * @return bool
     * @throws BlException
     */
    public function run()
    {
        $model = $this->getRoleBindingUserDb();
        $data = [
            $model->_uid=>$this->userObj->getUid(),
            $model->_role_id=>$this->roleObj->getRoleId(),
        ];
        if(!$this->userObj->getRoleClass()->getRoleId()){
            $this->bindingRole($data);
        }else{
            $this->editBindingRole($data);
        }
        return true;
    }

    /**
     * 新增
     * @param $data
     * @return bool
     * @throws BlException
     */
    private function bindingRole($data){
        $model = $this->getRoleBindingUserDb();
        $data[$model->_create_time] = time();
        $data[$model->_create_uid]  = $this->opUserObj->getUid();
        $result = $model->insert($data);
        if(!$result){
            throw new BlException('新增用户绑定角色失败');
        }
        return true;
    }

    /**
     * 修改用户角色绑定
     * @param $data
     * @return bool
     * @throws BlException
     */
    private function editBindingRole($data){
        $model = $this->getRoleBindingUserDb();
        $data[$model->_save_time] = time();
        $data[$model->_save_uid]  = $this->opUserObj->getUid();
        $result = $model->where($model->_uid,$this->userObj->getUid())->update($data);
        if(!$result){
            throw new BlException('修改用户绑定角色失败');
        }
        return true;
    }

    /**
     * 获取数据库
     * @return RoleUser
     */
    private function getRoleBindingUserDb(){
        if(empty($this->roleBindingUserDb)){
            $this->roleBindingUserDb = new RoleUser();
        }
        return $this->roleBindingUserDb;
    }

}